package com.example.cloud.controller.business.medical;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.dao.business.medical.SkinDiagnosis;
import com.example.cloud.model.basic.Diseases;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.service.basic.info.DiseasesService;
import com.example.cloud.service.business.medical.SkinDiagnosisService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/7/2.
 */
@RestController
@RequestMapping("/skin/diagnosis")
public class SkinDiagnosisController extends BaseController {

    @Autowired
    private DiseasesService diseasesService;

    @Autowired
    private SkinDiagnosisService skinDiagnosisService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        Result result = this.singleQuery(model, SkinDiagnosis.class);
        JSONArray datas = (JSONArray)result.getData();
        for (Object data : datas) {
            JSONObject json = (JSONObject)data;
            if (json.get("diagnosisResult").toString().length() > 2) {
              System.out.print(json.get("diagnosisResult").toString().length());
              String diagnosisResult = json.get("diagnosisResult").toString();
              JSONArray idsArr = JSON.parseArray(diagnosisResult);
              List<Long> ids = new ArrayList<>();
              for (Object id : idsArr) {
                  ids.add(((Integer) id).longValue());
              }
              StringBuilder s = new StringBuilder();
              List<Diseases> list = diseasesService.findByIds(ids);
              for (Diseases diseases : list) {
                  s.append(diseases.getName() + " , ");
              }
              json.put("diagnosisResult", s.toString().substring(0, s.toString().lastIndexOf(" , ")));
            }
        }
        
        return result;
    } 

    @GetMapping("/findById/{id}/{type}")
    public Result findById(@PathVariable Long id, @PathVariable int type){
        Map<String, Object> params = new HashMap();
        params.put("datas", diseasesService.findByTypeFromCache(type));
        params.put("data", skinDiagnosisService.findOne(id));
        return ResultUtil.success(null, params);
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody SkinDiagnosis record) {
        skinDiagnosisService.doSave(record);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids){
        skinDiagnosisService.doDelete(ids);
    }
}
